home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / oledoccl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  11.9 KB  |  466 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. #define new DEBUG_NEW
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // COleDocObjectItem
  22.  
  23. IMPLEMENT_DYNAMIC(COleDocObjectItem, COleClientItem)
  24.  
  25. BEGIN_INTERFACE_MAP(COleDocObjectItem, COleClientItem)
  26.     INTERFACE_PART(COleDocObjectItem, IID_IOleDocumentSite, OleDocumentSite)
  27. END_INTERFACE_MAP()
  28.  
  29.  
  30. COleDocObjectItem::COleDocObjectItem(COleDocument* pContainerDoc)
  31.     : COleClientItem(pContainerDoc)
  32. {
  33.     m_pHelpPopupMenu = NULL;
  34.     m_pActiveView = NULL;
  35.     m_pIPrint = NULL;
  36.     m_bInHelpMenu = FALSE;
  37. }
  38.  
  39. COleDocObjectItem::~COleDocObjectItem()
  40. {
  41.     if (m_pHelpPopupMenu != NULL)
  42.         m_pHelpPopupMenu->RemoveMenu(0, MF_BYPOSITION);
  43.     delete m_pHelpPopupMenu;
  44. }
  45.  
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // IOleDocumentSite interface
  49.  
  50. STDMETHODIMP_(ULONG) COleDocObjectItem::XOleDocumentSite::AddRef()
  51. {
  52.     METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  53.     return pThis->ExternalAddRef();
  54. }
  55.  
  56. STDMETHODIMP_(ULONG) COleDocObjectItem::XOleDocumentSite::Release()
  57. {
  58.     METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  59.     return pThis->ExternalRelease();
  60. }
  61.  
  62. STDMETHODIMP COleDocObjectItem::XOleDocumentSite::QueryInterface(
  63.     REFIID iid, LPVOID* ppvObj)
  64. {
  65.     METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  66.     return pThis->ExternalQueryInterface(&iid, ppvObj);
  67. }
  68.  
  69. STDMETHODIMP COleDocObjectItem::XOleDocumentSite::ActivateMe(
  70.     LPOLEDOCUMENTVIEW pViewToActivate)
  71. {
  72.     METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  73.  
  74.     LPOLEDOCUMENT lpDocument;
  75.     LPOLECLIENTSITE lpClientSite = pThis->GetClientSite();
  76.     LPOLEINPLACESITE lpInPlaceSite =
  77.         (LPOLEINPLACESITE) pThis->GetInterface(&IID_IOleInPlaceSite);
  78.  
  79.     if (lpClientSite == NULL || lpInPlaceSite == NULL)
  80.         return E_FAIL;
  81.  
  82.     // if we've gotten a NULL view, we're to create one ourselves
  83.  
  84.     if (pViewToActivate == NULL)
  85.     {
  86.         // if we already have a view, we can simply activate it
  87.  
  88.         if (pThis->m_pActiveView != NULL && pThis->m_pView != NULL)
  89.         {
  90.             pThis->ActivateAndShow();
  91.             return NOERROR;
  92.         }
  93.  
  94.         ASSERT(pThis->m_lpObject != NULL);
  95.         if (pThis->m_lpObject == NULL)
  96.             return E_FAIL;
  97.  
  98.         lpDocument = QUERYINTERFACE(pThis->m_lpObject, IOleDocument);
  99.         if (lpDocument == NULL)
  100.             return E_FAIL;
  101.  
  102.         if (FAILED(
  103.             lpDocument->CreateView(lpInPlaceSite, NULL, 0, &pViewToActivate)))
  104.         {
  105.             lpDocument->Release();
  106.           return E_OUTOFMEMORY;           
  107.         }
  108.  
  109.         // we're done with the document pointer
  110.         lpDocument->Release();
  111.     }
  112.     else if (pThis->m_pActiveView != NULL && pThis->m_pActiveView == pViewToActivate)
  113.     {
  114.         // we already own this view, so no need to addref
  115.         // simply make it visible and resize it
  116.  
  117.         pThis->ActivateAndShow();
  118.         return NOERROR;
  119.     }
  120.     else
  121.     {
  122.         // set the in-place site
  123.         pViewToActivate->SetInPlaceSite(lpInPlaceSite);
  124.         pViewToActivate->AddRef();
  125.     }
  126.  
  127.     // it must've created
  128.     ASSERT(pThis->m_pView != NULL);
  129.  
  130.     // if we had an old one, release it
  131.     if (pThis->m_pActiveView != NULL)
  132.     {
  133.         pThis->m_pActiveView->Show(FALSE);
  134.         pThis->m_pActiveView->UIActivate(FALSE);
  135.         pThis->m_pActiveView->Release();
  136.         RELEASE(pThis->m_pIPrint);
  137.     }
  138.  
  139.     // remember it for later
  140.     pThis->m_pActiveView = pViewToActivate;
  141.  
  142.     // activate and position it
  143.     pThis->ActivateAndShow();
  144.  
  145.     return NOERROR;
  146. }
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149. // IOleDocumentSite implementation helper
  150.  
  151. void COleDocObjectItem::ActivateAndShow()
  152. {
  153.     // set up toolbars and menus for the object
  154.     m_pActiveView->UIActivate(TRUE);
  155.  
  156.     // set the window size, avoiding new toolbars
  157.     RECT rc;
  158.     m_pView->GetClientRect(&rc);
  159.     m_pActiveView->SetRect(&rc);
  160.  
  161.     // make everything visible
  162.     m_pActiveView->Show(TRUE);
  163.  
  164.     return;
  165. }
  166.  
  167. LPOLEDOCUMENTVIEW COleDocObjectItem::GetActiveView() const
  168. {
  169.     return m_pActiveView;
  170. }
  171.  
  172. void COleDocObjectItem::Release(OLECLOSE dwCloseOption)
  173. {
  174.     RELEASE(m_pActiveView);
  175.     RELEASE(m_pIPrint);
  176.     COleClientItem::Release(dwCloseOption);
  177. }
  178.  
  179. HRESULT COleDocObjectItem::ExecCommand(DWORD nCmdID,
  180.     DWORD nCmdExecOpt /* = OLECMDEXECOPT_DONTPROMPTUSER */,
  181.     const GUID* pguidCmdGroup /* = NULL */)
  182. {
  183.     LPOLECOMMANDTARGET lpCt = QUERYINTERFACE(m_lpObject, IOleCommandTarget);
  184.     HRESULT hr = E_NOTIMPL;
  185.  
  186.     if (lpCt != NULL)
  187.         hr = lpCt->Exec(pguidCmdGroup, nCmdID, nCmdExecOpt, NULL, NULL);
  188.  
  189.     RELEASE(lpCt);
  190.     return hr;
  191. }
  192.  
  193. BOOL COleDocObjectItem::SupportsIPrint()
  194. {
  195.     if (m_pIPrint != NULL)
  196.         return TRUE;
  197.  
  198.     m_pIPrint = QUERYINTERFACE(m_lpObject, IPrint);
  199.     return (m_pIPrint != NULL);
  200. }
  201.  
  202. BOOL COleDocObjectItem::GetPageCount(LPLONG pnFirstPage, LPLONG pcPages)
  203. {
  204.     if (m_pIPrint == NULL && !SupportsIPrint())
  205.         return FALSE;
  206.  
  207.     //WINBUG: The proxy in DOCOBJ.DLL is broken; it doesn't allow
  208.     // NULL parameters to IPrint::GetPageInfo(), even though the spec
  209.     // says it should.
  210.  
  211.     LONG lPages;
  212.     LONG lFirstPage;
  213.  
  214.     HRESULT hr = m_pIPrint->GetPageInfo(&lFirstPage, &lPages);
  215.  
  216.     if (pnFirstPage != NULL)
  217.         *pnFirstPage = lFirstPage;
  218.     if (pcPages != NULL)
  219.         *pcPages = lPages;
  220.  
  221.     if (SUCCEEDED(hr))
  222.         return TRUE;
  223.     else
  224.         return FALSE;
  225. }
  226.  
  227.  
  228. CMenu* COleDocObjectItem::GetHelpMenu(UINT& nPosition)
  229. {
  230.     CFrameWnd* pFrame = m_pView->GetTopLevelFrame();
  231.     CMenu* pMenuFrame = CMenu::FromHandle(pFrame->m_hMenuDefault);
  232.  
  233.     if (pMenuFrame != NULL)
  234.         nPosition = pMenuFrame->GetMenuItemCount() -1;
  235.  
  236.     return pMenuFrame;
  237. }
  238.  
  239. void COleDocObjectItem::OnInsertMenus(CMenu* pMenuShared,
  240.     LPOLEMENUGROUPWIDTHS lpMenuWidths)
  241. {
  242.     ASSERT_VALID(this);
  243.     ASSERT_VALID(pMenuShared);
  244.     ASSERT(AfxIsValidAddress(lpMenuWidths, sizeof(OLEMENUGROUPWIDTHS)));
  245.  
  246.     // initialize the group widths array
  247.     lpMenuWidths->width[0] = 1;
  248.     lpMenuWidths->width[2] = 0;
  249.     lpMenuWidths->width[4] = 0;
  250.  
  251.     // get menu from document template
  252.     CDocTemplate* pTemplate = GetDocument()->GetDocTemplate();
  253.     HMENU hMenuOLE = pTemplate->m_hMenuInPlace;
  254.  
  255.     // only copy the popups if there is a menu loaded
  256.     if (hMenuOLE == NULL)
  257.         return;
  258.  
  259.     UINT nItem;
  260.     CMenu* pMenuFrame = GetHelpMenu(nItem);
  261.  
  262.     if (pMenuFrame != NULL)
  263.     {
  264.         CString strHelpMenuName;
  265.         int nSeparator = pMenuFrame->GetMenuString(nItem,
  266.                 strHelpMenuName, MF_BYPOSITION);
  267.         if (nSeparator == 0)
  268.         {
  269.             TRACE0("Error: COleDocObjectItem::OnInsertMenus() found no help menu!\n");
  270.             return;
  271.         }
  272.  
  273.         CString strTearOffName;
  274.         strTearOffName.Format(_T("%s %s"), AfxGetAppName(), strHelpMenuName);
  275.         strTearOffName.Remove('&');
  276.  
  277.         // get the normal frame menu
  278.         int nCount = pMenuFrame->GetMenuItemCount();
  279.         HMENU hMenu = GetSubMenu(pMenuFrame->m_hMenu, nCount-1);
  280.  
  281.         // clean up old menu and allocate a new one
  282.         if (m_pHelpPopupMenu == NULL)
  283.         {
  284.             m_pHelpPopupMenu = new CMenu;
  285.  
  286.             // create new sub-popup menu and add container's Help tearoff
  287.             // then add help menu from main window
  288.             m_pHelpPopupMenu->CreateMenu();
  289.             m_pHelpPopupMenu->InsertMenu((UINT) -1, MF_BYPOSITION | MF_POPUP,
  290.                 (UINT) hMenu, strTearOffName);
  291.         }
  292.  
  293.         pMenuShared->InsertMenu(1, MF_BYPOSITION | MF_POPUP,
  294.             (UINT) m_pHelpPopupMenu->m_hMenu, strHelpMenuName);
  295.  
  296.         // tell the object we added our Help menu
  297.         lpMenuWidths->width[5] = 1;
  298.     }
  299.  
  300.     // insert our menu items and adjust group widths array
  301.     AfxMergeMenus(pMenuShared->GetSafeHmenu(), hMenuOLE,
  302.         &lpMenuWidths->width[0], 0);
  303. }
  304.  
  305. void COleDocObjectItem::OnRemoveMenus(CMenu *pMenuShared)
  306. {
  307.     int cItemsShared = pMenuShared->GetMenuItemCount();
  308.     if (cItemsShared != 0)
  309.     {
  310.         CMenu *pMenuHelp = pMenuShared->GetSubMenu(cItemsShared - 1);
  311.     
  312.         int cItemsHelp = pMenuHelp->GetMenuItemCount();
  313.         int nItem;
  314.         for (nItem = cItemsHelp-1; nItem > 0; nItem--)
  315.             pMenuHelp->DeleteMenu(nItem, MF_BYPOSITION);
  316.  
  317.         pMenuShared->RemoveMenu(cItemsShared - 1, MF_BYPOSITION);
  318.     }
  319.  
  320.     COleClientItem::OnRemoveMenus(pMenuShared);
  321. }
  322.  
  323. BOOL COleDocObjectItem::OnPreparePrinting(CView* pCaller,
  324.     CPrintInfo* pInfo, BOOL bPrintAll)
  325. {
  326.     LONG lDocObjectPages = 0;
  327.  
  328.     CDocument* pDoc = pCaller->GetDocument();
  329.     COleDocument* pOleDoc = DYNAMIC_DOWNCAST(COleDocument, pDoc);
  330.     if (pOleDoc == NULL)
  331.         return FALSE;
  332.  
  333.     POSITION pos = pOleDoc->GetStartPosition();
  334.     while (pos != NULL)
  335.     {
  336.         COleClientItem* pItem = pOleDoc->GetNextClientItem(pos);
  337.         COleDocObjectItem* pDocItem =
  338.             DYNAMIC_DOWNCAST(COleDocObjectItem, pItem);
  339.         if (pDocItem == NULL)
  340.             continue;
  341.  
  342.         // if this isn't the view, continue
  343.         if (!bPrintAll)
  344.         {
  345.             if (pItem->m_pView == NULL ||
  346.                  pItem->m_pView->m_hWnd != pCaller->m_hWnd)
  347.                 continue;
  348.         }
  349.  
  350.         if (pDocItem->SupportsIPrint())
  351.         {
  352.             LONG lThisObjectPages;
  353.             if (pDocItem->GetPageCount(NULL, &lThisObjectPages))
  354.                 lDocObjectPages += lThisObjectPages;
  355.             pInfo->m_bDocObject = TRUE;
  356.         }
  357.         else
  358.             lDocObjectPages++;
  359.  
  360.         if (!bPrintAll)
  361.             break;
  362.     }
  363.  
  364.     if (lDocObjectPages > 0)
  365.     {
  366.         UINT nMaxPage = pInfo->GetMaxPage();
  367.  
  368.         // set the page count; increment it if previously set
  369.         if (nMaxPage == 0xFFFF)
  370.             pInfo->SetMaxPage(lDocObjectPages);
  371.         else
  372.             pInfo->SetMaxPage(nMaxPage + lDocObjectPages);
  373.         pInfo->m_bDocObject = TRUE;
  374.     }
  375.  
  376.     return TRUE;    
  377. }
  378.  
  379. void COleDocObjectItem::OnPrint(CView* pCaller,    CPrintInfo* pInfo,
  380.     BOOL bPrintAll)
  381. {
  382.     CDocument* pDoc = pCaller->GetDocument();
  383.     COleDocument* pOleDoc = DYNAMIC_DOWNCAST(COleDocument, pDoc);
  384.     if (pOleDoc == NULL)
  385.         return;
  386.  
  387.     POSITION pos = pOleDoc->GetStartPosition();
  388.     while (pos != NULL)
  389.     {
  390.         COleClientItem* pItem = pOleDoc->GetNextClientItem(pos);
  391.         COleDocObjectItem* pDocItem = DYNAMIC_DOWNCAST(COleDocObjectItem, pItem);
  392.         if (pDocItem == NULL)
  393.             continue;
  394.  
  395.         // if this isn't the view, continue
  396.         if (!bPrintAll)
  397.         {
  398.             if (pItem->m_pView == NULL || pItem->m_pView->m_hWnd != pCaller->m_hWnd)
  399.                 continue;
  400.         }
  401.  
  402.         HRESULT hrThisPage = E_UNEXPECTED;
  403.  
  404.         if (pDocItem->SupportsIPrint())
  405.         {
  406.             DVTARGETDEVICE* pTargetDevice = NULL;
  407.             LPDEVNAMES lpDevNames = NULL;
  408.             LPDEVMODE lpDevMode = NULL;
  409.  
  410.             lpDevNames = (LPDEVNAMES) GlobalLock(pInfo->m_pPD->m_pd.hDevNames);
  411.             if (lpDevNames != NULL)
  412.             {
  413.                 lpDevMode = (LPDEVMODE) GlobalLock(pInfo->m_pPD->m_pd.hDevMode);
  414.                 if (lpDevMode != NULL)
  415.                 {
  416.                     pTargetDevice = _AfxOleCreateTargetDevice(lpDevNames, lpDevMode);
  417.                     if (pTargetDevice != NULL)
  418.                     {
  419.                         PAGESET* pps = (PAGESET*) CoTaskMemAlloc(sizeof(PAGESET));
  420.                         if (pps != NULL)
  421.                         {
  422.                             pps->cbStruct = sizeof(PAGESET);
  423.                             ASSERT((pps->cbStruct % 4) == 0);
  424.                             pps->fOddPages = TRUE;
  425.                             pps->fEvenPages = TRUE;
  426.                             pps->cPageRange = 1;
  427.                             pps->rgPages[0].nFromPage = pInfo->m_nCurPage;
  428.                             pps->rgPages[0].nToPage = pInfo->m_nCurPage;
  429.  
  430.                             LONG lLastPage = pInfo->m_nCurPage;
  431.                             LONG lPagesPrinted;
  432.  
  433.                             DWORD dwFlags = PRINTFLAG_RECOMPOSETODEVICE;
  434.  
  435.                             if (pInfo->m_pPD->m_pd.Flags & PD_PRINTTOFILE)
  436.                                 dwFlags |= PRINTFLAG_PRINTTOFILE;
  437.  
  438.                             hrThisPage = pDocItem->m_pIPrint->Print(dwFlags,
  439.                                     &pTargetDevice, &pps, NULL, NULL,
  440.                                     pInfo->m_nCurPage, &lPagesPrinted,
  441.                                     &lLastPage);
  442.  
  443.                             if (!SUCCEEDED(hrThisPage))
  444.                                 TRACE1("IPrint::Print() returned %8.8X\n", hrThisPage); 
  445.                             CoTaskMemFree(pps);
  446.                         }
  447.                         CoTaskMemFree(pTargetDevice);
  448.                     }
  449.                     GlobalUnlock(pInfo->m_pPD->m_pd.hDevMode);
  450.                 }
  451.                 GlobalUnlock(pInfo->m_pPD->m_pd.hDevNames);
  452.             }
  453.         }
  454.         else
  455.         {
  456.             // try through IOleCommandTarget
  457.  
  458.             hrThisPage = pDocItem->ExecCommand(OLECMDID_PRINT);
  459.             if (!SUCCEEDED(hrThisPage))
  460.                 TRACE1("IOleCommandTarget::Exec() returned %8.8X\n", hrThisPage); 
  461.         }
  462.     }
  463.  
  464.     return;
  465. }
  466.